package com.gamiao.gwt.demo.useSpring.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class UserEditorPanelWidget extends VerticalPanel {

	private Label message;

	private TextBox userSaveSucceedTag;

	private TextBox userID;

	private TextBox firstName;

	private TextBox lastName;

	private Button save;

	private FlexTable layout;

	private UserServiceAsync userService;

	private UserSaveEventListenerCollection userSaveEventListeners;

	private UserSaveSucceedEventListenerCollection userSaveSucceedEventListeners;

	public UserEditorPanelWidget(String userID_str, String firstName_str,
			String lastName_str) {
		message = new Label();
		userID = new TextBox();
		firstName = new TextBox();
		lastName = new TextBox();
		save = new Button("Save");
		layout = new FlexTable();
		userSaveSucceedTag = new TextBox();

		userID.setText(userID_str);
		userID.setEnabled(false);
		firstName.setText(firstName_str);
		lastName.setText(lastName_str);
		save.addClickListener(new SaveButtonClickListener());
		userSaveSucceedTag.setText("Success!");
		userSaveSucceedTag.setVisible(false);

		layout.setText(0, 0, "User ID:");
		layout.setWidget(0, 1, userID);
		layout.setText(1, 0, "First name:");
		layout.setWidget(1, 1, firstName);
		layout.setText(2, 0, "Last name:");
		layout.setWidget(2, 1, lastName);

		this.add(message);
		this.setCellHorizontalAlignment(message, VerticalPanel.ALIGN_CENTER);
		this.add(layout);
		this.setCellHorizontalAlignment(layout, VerticalPanel.ALIGN_CENTER);
		this.add(save);
		this.setCellHorizontalAlignment(save, VerticalPanel.ALIGN_CENTER);
	}

	public void setUserInfo(String userID_str, String firstName_str,
			String lastName_str) {
		userID.setText(userID_str);
		firstName.setText(firstName_str);
		lastName.setText(lastName_str);
	}

	public class SaveButtonClickListener implements ClickListener {
		public void onClick(Widget arg0) {
			getUserClientService();
			PersonClient personClient = new PersonClient();
			personClient.setId(Integer.valueOf(userID.getText()).intValue());
			personClient.setFirstName(firstName.getText());
			personClient.setLastName(lastName.getText());
			userService.save(personClient, new SaveUserCallback());
		}
	}

	public class SaveUserCallback implements AsyncCallback {
		public void onFailure(Throwable error) {
			message.setText("Save user Failed!");
		}

		public void onSuccess(Object result) {
			userSaveSucceedEventListeners.fireChange(userSaveSucceedTag);
			message.setText("Save user Success!");
		}
	}

	private void getUserClientService() {
		if (userService == null) {
			userService = UserService.Util.getInstance();
		}
	}

	public final void addUserSaveEventListener(ClickListener listener) {
		if (userSaveEventListeners == null) {
			userSaveEventListeners = new UserSaveEventListenerCollection(this,
					save);
		}
		userSaveEventListeners.add(listener);
	}

	public final void addUserSaveSucceedEventListenerCollection(
			ChangeListener listener) {
		if (userSaveSucceedEventListeners == null) {
			userSaveSucceedEventListeners = new UserSaveSucceedEventListenerCollection(
					this, userSaveSucceedTag);
		}
		userSaveSucceedEventListeners.add(listener);
	}
}
